package com.camera2.http;

import android.content.Context;
import android.util.Log;

import com.camera2.Utils.StreamUtils;
import com.camera2.local.SharedPrefer;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONObject;

import java.io.InputStream;

public class UpdatePassword {

	public static String update(Context context, String username, String oldPassword,String newPassword)
			throws Exception {
		String token = SharedPrefer.ReadToken(context);
		HttpClient client = new DefaultHttpClient();
		HttpPost httpPost = new HttpPost(GlobalUrl.getUpdatepasswordUrl());
		httpPost.setHeader("token", token);
		httpPost.addHeader("Content-Type", "application/json");
		JSONObject jsonParam = new JSONObject();
		jsonParam.put("mobile", username);
		jsonParam.put("newPassword", newPassword);
		jsonParam.put("oldPassword", oldPassword);

		StringEntity entity = new StringEntity(jsonParam.toString(),"utf-8");//解决中文乱码问题
		entity.setContentEncoding("UTF-8");
		entity.setContentType("application/json");
		 httpPost.setEntity(entity);

		HttpResponse response = client.execute(httpPost);
		int code = response.getStatusLine().getStatusCode();
		InputStream is = response.getEntity().getContent();
		String result = StreamUtils.readStream(is);
		Log.i("UpdatePassword", "UpdatePassword: "+result);
		JSONObject jsonObj = new JSONObject(result);
		String msg = jsonObj.getString("msg");

		return msg;
	}
}
